<?php
/**
 * View.class.php
 * - Use contain function view to templates of project
 * @author RUNSYSTEM
 * @since version - Aug 04, 2011
 * $Id: $
 */
class ViewAdmin {
	/**
	 * __construct
	 * - Use constructor object
	 * @param $controller: object want view to template
	 */
	public function __construct ($controller = null) {
		if ($controller != null) {
			$this->assignobj($controller);	
		}	
	}
	
	/**
	 * assignView
	 * - Use convert one properties to template
	 * @param string $key: name of properties
	 * @param string $value: value of properties 
	 */
    public function assignView($key, $value) {
        $this->$key = $value;
    }
    
    /**
     * display
     * - Use get template of object
     * @param string $template: name of template
     */
    public function display($template = '') {
    	// Get template header
	    $this->requireTpl('header');	
    	// Get template left
	    $this->requireTpl('left');	
	    // Get template content
	    $template = $template == '' ? $this->templateFile : $template; 
	    $this->requireTpl($template);
	    // Get template footer
	    $this->requireTpl('footer');	
		exit;  
    }
    
    /**
     * requireTpl
     * - Use require file template
     * @param string $templateName: name of tempate
     */
    public function requireTpl ($templateName) {
    	$template_tpl = MODULE_DIR . "/admin/templates/" . $templateName . ".tpl.php";
    	if (is_file($template_tpl)) {
			require($template_tpl);
		}	
    }
    /**
     * escapeData
     * - Use escape data after view templates
     * @param $data : is array data or string data
     * @return $data if data is string, callback function if data is array
     */
    public function escapeData($data) {
    	// Escape data
    	if (!is_array($data)) {
    		return htmlspecialchars($data);
    	}
    	// Callback function if data is array
    	if (is_array($data)) {
    		return array_map(array($this, "escapeData"), $data);
    	}
    }
    /**
     * assignobj
     * - Use convert info of object to view properties
     * @param $obj: object want view to template
     * @param boolen $flgEscape: flag check escape data
     */
	public function assignobj($obj) {
		// Return an array of object properties
        $data = get_object_vars($obj);
        // Convert info of object to view properties
        foreach ($data as $key => $value){
        	$this->$key = $value;
        }
    }
}

?>
